package cn.stylefeng.guns.modular.fileTag.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.fileTag.model.FileTagParam;
import cn.stylefeng.guns.modular.fileTag.model.FileTagResult;
import cn.stylefeng.guns.modular.fileTag.service.FileTagService;
import cn.stylefeng.guns.modular.studentmanage.model.LeaveUserResult;
import cn.stylefeng.guns.sys.modular.system.entity.FileTagPO;
import cn.stylefeng.guns.sys.modular.system.warpper.LogWrapper;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/fileTag")
public class FileTagController {

    private String PREFIX = "/fileTag";

    @Autowired
    private FileTagService fileTagService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/fileTag.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/fileTag_add.html";
    }

    /**
     * 修改页面
     */
    @RequestMapping("/fileTag_update")
    public String deptUpdate() {
        return PREFIX + "/fileTag_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FileTagPO fileTagPO) {
        fileTagPO.setCreateTime(new Date().getTime() / 1000);
        fileTagPO.setCreateUserId(LoginContextHolder.getContext().getUserId());
        fileTagPO.setCreateUserName(LoginContextHolder.getContext().getUser().getName());
        fileTagService.consummateInfo(fileTagPO);
        boolean b = fileTagService.save(fileTagPO);
        if (b) {
            return ResponseData.success(200, "修改成功", null);
        }
        return ResponseData.success(500, "修改失败", null);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FileTagParam fileTagParam) {
        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();
        List<FileTagResult> fileTagResults = fileTagService.processList(page, fileTagParam);
        List<FileTagResult> collects = null;
        //权限数据过滤
        //数据与当前登录用户id比较
        List<FileTagResult> collects2 = null;
        collects = fileTagResults.stream().filter(fileTagResult -> fileTagResult.getCreateUserId().equals(LoginContextHolder.getContext().getUserId())).collect(Collectors.toList());
        //如若登录的是院系领导
        if (collects.size() == 0) {
            collects2 = fileTagResults.stream().filter(fileTagResult -> fileTagResult.getFoxbaseApprovalId().equals(LoginContextHolder.getContext().getUserId())).collect(Collectors.toList());
            if (collects2.size() == 0) {
                //校级领导查询全部
                collects2 = fileTagResults.stream().filter(fileTagResult -> fileTagResult.getTertiaryApprovalId().equals(LoginContextHolder.getContext().getUserId())).collect(Collectors.toList());
            }
        } else {
            collects2 = collects;
        }
        page.setRecords(collects2);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 文件详情
     */
    @ResponseBody
    @RequestMapping(value = "/detail")
    public Object detail(Long id) {
        FileTagPO fileTagPO = fileTagService.getById(id);
        Map<String, Object> noticeMap = BeanUtil.beanToMap(fileTagPO);
        return ResponseData.success(noticeMap);
    }

    /**
     * 修改文件
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public ResponseData update(FileTagPO fileTagPO) {
        fileTagPO.setUpdateTime(new Date().getTime() / 1000);
        fileTagPO.setUpdateUserId(LoginContextHolder.getContext().getUserId());
        fileTagPO.setUpdateUserName(LoginContextHolder.getContext().getUser().getName());
        boolean b = fileTagService.updateById(fileTagPO);
        if (b) {
            return ResponseData.success(200, "修改成功", null);
        }
        return ResponseData.success(500, "修改失败", null);
    }

    /**
     * 删除记录（删除）
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FileTagParam fileTagParam) {
        boolean b = fileTagService.removeById(fileTagParam.getId());
        if (b) {
            return ResponseData.success(200, "删除成功", null);
        }
        return ResponseData.success(500, "删除失败", null);
    }

    @ResponseBody
    @RequestMapping("/getTaglist/{type}")
    public Page<FileTagPO> getTaglist(@PathVariable("type") Integer type) {
        List<FileTagPO> fileTagPOS = fileTagService.list();
        List<FileTagPO> fileTagPOResult = null;
        if (1 == type) {
            fileTagPOResult = fileTagPOS.stream().filter(fileTagPO -> fileTagPO.getType() == 1).collect(Collectors.toList());
        } else if (2 == type) {
            fileTagPOResult = fileTagPOS.stream().filter(fileTagPO -> fileTagPO.getType() == 2).collect(Collectors.toList());
        } else if (3 == type) {
            FileTagPO fileTagPO1 = new FileTagPO();
            fileTagPO1.setId((long) 1);
            fileTagPO1.setTagName("项目标签");
            FileTagPO fileTagPO2 = new FileTagPO();
            fileTagPO2.setId((long) 2);
            fileTagPO2.setTagName("技术栈标签");
            fileTagPOResult = new ArrayList<>(2);
            fileTagPOResult.add(fileTagPO1);
            fileTagPOResult.add(fileTagPO2);
        }
        Page<FileTagPO> egFormPage = new Page<>();
        egFormPage.setRecords(fileTagPOResult);
        return egFormPage;
    }

    @ResponseBody
    @RequestMapping("/getClasslist")
    public Page<FileTagPO> getClasslist() {
        List<FileTagPO> fileTagPOS = fileTagService.list();
        List<FileTagPO> fileTagPOResult = fileTagPOS.stream().filter(fileTagPO ->
                fileTagPO.getCreateUserId().equals(LoginContextHolder.getContext().getUserId())
        ).collect(Collectors.toList());
        Page<FileTagPO> egFormPage = new Page<>();
        egFormPage.setRecords(fileTagPOResult);
        return egFormPage;
    }
}